//
//  SLLanguageSelectView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/9.
//

#import "SLLanguageSelectView.h"
@interface SLLanguageSelectView(){
    NSInteger _curIndex;
}

@end

@implementation SLLanguageSelectView
-(void)awakeFromNib{
    [super awakeFromNib];
    
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    
    _langBgView.layer.cornerRadius = 20;
    _langBgView.layer.masksToBounds = YES;
    
    [_saveBtn setTitle:kLocalString(@"Save") forState:UIControlStateNormal];
    _saveBtn.btnStyle = 1;
    _tipLbl.text = kLocalString(@"Change Language");
    
    _lang1Lbl.text = @"English";
    _lang2Lbl.text = @"بالعربية";
    _listView.layer.cornerRadius = 2;
    _listView.layer.masksToBounds = YES;
    
    kWeakSelf(self);
    _langView1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself changeLang:1];
    }];
    [_langView1 addGestureRecognizer:tap1];
    
    _langView2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself changeLang:2];
    }];
    [_langView2 addGestureRecognizer:tap2];
    
    _grayView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself removeWithAnimation];
    }];
    [_grayView addGestureRecognizer:tap3];
    [self updateWithCurLang];
    
    _hideView.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDown:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionDown;
    [_hideView addGestureRecognizer:swipeRight];
}

// 手势处理方法
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)gesture {
    [self removeWithAnimation];
}

-(void)setStyle:(NSInteger)style{
    _style = style;
    if (style == 2) {//温度单位设置样式
        _tipLbl.text = kLocalString(@"Temperature unit");
        _lang1Lbl.text = kLocalString(@"ºC  Celcius");
        _lang2Lbl.text = kLocalString(@"ºF  Fahrenheit");
        NSString *curTempUnit = kSTRING([[NSUserDefaults standardUserDefaults] objectForKey:@"curTempUnit"]);
        if ([curTempUnit isEqualToString:@"F"]) {
            _lang1Lbl.textColor = KWhiteColor_text;
            _lang2Lbl.textColor = KColor_O;
            _lang1Icon.hidden = YES;
            _lang2Icon.hidden = NO;
        }else{
            _lang1Lbl.textColor = KColor_O;
            _lang2Lbl.textColor = KWhiteColor_text;
            _lang2Icon.hidden = YES;
            _lang1Icon.hidden = NO;
        }
    }
}

-(void)changeLang:(NSInteger)index{
    if (_curIndex == index) {
        return;
    }
    _curIndex = index;
    if (_curIndex == 1) {
        _lang1Lbl.textColor = KColor_O;
        _lang1Icon.hidden = NO;
        _lang2Lbl.textColor = KWhiteColor_text;
        _lang2Icon.hidden = YES;
    }else if (_curIndex == 2){
        _lang1Lbl.textColor = KWhiteColor_text;
        _lang1Icon.hidden = YES;
        _lang2Lbl.textColor = KColor_O;
        _lang2Icon.hidden = NO;
    }
}

-(void)updateWithCurLang{
    
    if (kCurLangIsEnglish) {
        _lang1Lbl.textColor = KColor_O;
        _lang2Lbl.textColor = KWhiteColor_text;
        _lang2Icon.hidden = YES;
        _lang1Icon.hidden = NO;
    }else{
        _lang1Lbl.textColor = KWhiteColor_text;
        _lang2Lbl.textColor = KColor_O;
        _lang1Icon.hidden = YES;
        _lang2Icon.hidden = NO;
    }
    
}

- (IBAction)saveAction:(id)sender {
    if (_style == 2) {
        if (!_lang1Icon.hidden) {
            [[NSUserDefaults standardUserDefaults] setObject:@"C" forKey:@"curTempUnit"];
        }else if(!_lang2Icon.hidden){
            [[NSUserDefaults standardUserDefaults] setObject:@"F" forKey:@"curTempUnit"];
        }
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }else{
        if (!_lang1Icon.hidden) {
            [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"curLang"];
        }else if(!_lang2Icon.hidden){
            [[NSUserDefaults standardUserDefaults] setObject:@"ar" forKey:@"curLang"];
        }
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    [self removeWithAnimation];
    if (self.langChangeBlock) {
        self.langChangeBlock();
    }
}



@end
